/**
 * 快速获取当前时间
 * @param {String} str ('YmdHis') ('Y-m-d H:i:s')('Y-m-d ')
 * @returns String
 */
 function format(str){
     var d = new Date()

     str = str.replaceAll('Y', d.getFullYear())
     str = str.replaceAll('m', d.getMonth()+1)
     str = str.replaceAll('d', d.getDate())
     str = str.replaceAll('H', d.getHours())
     str = str.replaceAll('i', d.getMinutes())
     str = str.replaceAll('s', d.getSeconds())
     return str
 }

/**
 * 获取输入时间距今时间
 * @param {String} ('2021-10-5 12:00:00')
 * @returns String
 */
 function dateFormat(time){
    
    let nowTime = new Date()
    let nowTimes = nowTime.getTime()
    let oldTime = new Date(time)
    let oldTimes =oldTime.getTime()
    let diff = (nowTimes - oldTimes )/1000

    if(diff < 10) return '刚刚'
    if(diff < 60) return `${diff}秒前`
    if(diff < 60*60) return `${Math.floor(diff / 60)}分钟前`
    if(diff < 60*60*60) return `${Math.floor(diff / 60/60)}小时前`
    if(diff < 60*60*60*24) return `${Math.floor(diff / 60/60/24)}天前`
    if(diff < 60*60*60*24*30) return `${Math.floor(diff / 60/60/24/30)}月前`
    if(diff < 60*60*60*24*30*12) return `${Math.floor(diff / 60/60/24/30/12)}年前`

}


/**
 * 字符串补零 （传递数字不够2位 补0）
 * @param {String|Number}  '1,2,3,4'
 * @returns String
*/
function zeroize(data){
    return String(data).length === 1 ? '0'+data : data
}



/**
 * 随机十六进制颜色
 * @returns String
 */
 function randomColor() {
     var colorArr = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F'] 
     var min = 0
     var max = colorArr.length-1

     var first = colorArr[ Math.floor(Math.random()*(max-min+1)+min) ]
     var two = colorArr[ Math.floor(Math.random()*(max-min+1)+min) ]
     var three = colorArr[ Math.floor(Math.random()*(max-min+1)+min) ]
     var four = colorArr[ Math.floor(Math.random()*(max-min+1)+min) ]
     var five = colorArr[ Math.floor(Math.random()*(max-min+1)+min) ]
     var six = colorArr[ Math.floor(Math.random()*(max-min+1)+min) ]

     return '#'+first+two+three+four+five+six
 }
 

 /**
 * 判断浏览器类型
 * @param {} 
 * @returns String
*/
function getOs(){
    if(navigator.userAgent.indexOf('Firebox')!== -1) return '火狐浏览器'
    if(navigator.userAgent.indexOf('Chrome')!== -1) return '谷歌浏览器'
    if(navigator.userAgent.indexOf('Safari')!== -1) return '苹果浏览器'
    if(navigator.userAgent.indexOf('Opera')!== -1) return 'Opera浏览器'
    if(navigator.userAgent.indexOf('Trident')!== -1) return 'IE浏览器'
}

/**
 * 判断是PC还是移动端
 * @param {} 
 * @returns String
*/
function getPCMob(){
    data = navigator.userAgent
    if ((data.indexOf('Mac')||data.indexOf('Windows')||data.indexOf('Linux')) !== -1) return '电脑端'
    if ((data.indexOf('iPhone')||data.indexOf('iPad')||data.indexOf('Android')||data.indexOf('Mobile')) !== -1) return '移动端'
}


/**
 * 判断是否是微信内置浏览器
 * @param {} 
 * @returns String
*/
function getWX(){

    if(navigator.userAgent.indexOf('micromessenger')!== -1) return '微信内置浏览器'
    else return '不是微信内置浏览器'
}

/**
 * 获取非行内标签样式浏览器兼容问题解决
 * @param {Object,String} ('divObj','color')('divObj','fontSizr')
 * @returns String
 */
function getStyle(Obj,cssAttr){

    if (window.getComputedStyle)return getComputedStyle(Obj)[cssAttr]
    else return Obj.currenStyle[cssAttr]
}